package com.fc.test.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.DateUtil;
import com.fc.test.util.OrderNOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service
* @Title: TAbbQuanchengService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-15 15:47:57  
 */
@Service
public class TAbbQuanchengService implements BaseService<TAbbQuancheng, TAbbQuanchengExample>{
	@Autowired
	private TAbbQuanchengMapper tAbbQuanchengMapper;

	@Autowired
	private TAbbMemberMapper memberMapper;

	@Autowired
	private TAbbOrderMapper orderMapper;

	@Autowired
	private TAbbCommissionMapper tAbbCommissionMapper;

	@Autowired
	private TAbbQczifeiMapper qczifeiMapper;

	@Autowired
	private TAbbJquanchengMapper tAbbJquanchengMapper;
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<TAbbQuancheng> list(Tablepar tablepar,String name){
	        TAbbQuanchengExample testExample=new TAbbQuanchengExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andTitleLike("%"+name+"%");
	        }
		    testExample.createCriteria().andPaystatusEqualTo("2");
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TAbbQuancheng> list= tAbbQuanchengMapper.selectByExample(testExample);
	        PageInfo<TAbbQuancheng> pageInfo = new PageInfo<TAbbQuancheng>(list);
	        return  pageInfo;
	 }

	@Override
	@Transactional
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		for (String id:lista){
			TAbbOrderExample example=new TAbbOrderExample();
			TAbbOrderExample.Criteria criteria=example.createCriteria();
			criteria.andObjidEqualTo(id);
			criteria.andTypeEqualTo(1);
			List<TAbbOrder> orderList=orderMapper.selectByExample(example);
			if (orderList!=null && orderList.size()>0){
              for (TAbbOrder tAbbOrder:orderList){
              	  if (tAbbOrder.getRenwumoney()>0){
              	   	//返回任务余额到用户账号
					  TAbbMember tAbbMember=memberMapper.selectByPrimaryKey(tAbbOrder.getUserid());
					  TAbbMember tAbbMember3=new TAbbMember();
					  tAbbMember3.setId(tAbbMember.getId());
					  tAbbMember3.setAccount(tAbbMember.getAccount()+tAbbOrder.getRenwumoney());
					  memberMapper.updateByPrimaryKey(tAbbMember3);

					  //返回任务余额到用户账号的记录
					  TAbbCommission tAbbCommission3=new TAbbCommission();
					  tAbbCommission3.setCreattime(DateUtil.getCurrentDateTimeStr());
					  tAbbCommission3.setType(1);
					  tAbbCommission3.setBeiyongone(String.valueOf(tAbbOrder.getRenwumoney()));
					  tAbbCommission3.setBusinessid("3");
					  tAbbCommission3.setMemberid(tAbbMember.getId());
					  tAbbCommission3.setMessage("消除任务");
					  tAbbCommission3.setId(SnowflakeIdWorker.getUUID());
					  tAbbCommissionMapper.insertSelective(tAbbCommission3);
				  }
			  }
			}
		}
		TAbbQuanchengExample example=new TAbbQuanchengExample();
		example.createCriteria().andIdIn(lista);
		return tAbbQuanchengMapper.deleteByExample(example);
	}


	@Override
	public TAbbQuancheng selectByPrimaryKey(String id) {

		return tAbbQuanchengMapper.selectByPrimaryKey(id);
	}


	@Override
	public int updateByPrimaryKeySelective(TAbbQuancheng record) {
		return tAbbQuanchengMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbQuancheng record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbQuanchengMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(TAbbQuancheng record, TAbbQuanchengExample example) {

		return tAbbQuanchengMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbQuancheng record, TAbbQuanchengExample example) {

		return tAbbQuanchengMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbQuancheng> selectByExample(TAbbQuanchengExample example) {

		return tAbbQuanchengMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbQuanchengExample example) {

		return tAbbQuanchengMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbQuanchengExample example) {

		return tAbbQuanchengMapper.deleteByExample(example);
	}


	public List<TAbbQuancheng> getQuanchengByPage(int pageIndex,String memberId){
		Map<String,Object> paramMap = new HashMap<>();
		Map<String,Object> map = new HashMap<>();
		paramMap.put("pageIndex",pageIndex);
		List<TAbbQuancheng> list = tAbbQuanchengMapper.getQuanchengByPage(paramMap);
		TAbbQczifei qczifei = qczifeiMapper.SelectLimitone();
		DecimalFormat df = new DecimalFormat("#.00");

		for(TAbbQuancheng quancheng:list){
			double money = Double.parseDouble(qczifei.getMoney());
			double pingtaiscore = Double.parseDouble(qczifei.getPingtaiscore());
			double commission = money*((100-pingtaiscore)/100);
			quancheng.setCommission(Double.parseDouble(df.format(commission)));
			String username = quancheng.getUsername();
			if(null==username||"".equals(username)){
				quancheng.setUsername(quancheng.getMembername());
				quancheng.setUserid(quancheng.getMemberid());
			}

			map.put("userid",memberId);
			map.put("qcid",quancheng.getId());
			int receive = tAbbJquanchengMapper.isHaveNotSubmit(map);
			if(receive>0){
				quancheng.setIsReceive(1);
			}else{
				quancheng.setIsReceive(0);
			}
		}
		return list;
	}

	public Map<String,Object> getQuanchengDetail(String id,String userid){
		Map<String,Object> resMap = new HashMap<>();
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("qcid",id);
		paramMap.put("userid",userid);
		int countToday = tAbbJquanchengMapper.isReceivedToday(paramMap);
		int notSubmit = tAbbJquanchengMapper.isHaveNotSubmit(paramMap);
		if(countToday>0||notSubmit>0){
			resMap.put("flag",0);//今天领过或者之前领过未完成

			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//查询是否有待提交和审核不通过的
				TAbbJquancheng tAbbJquancheng = tAbbJquanchengMapper.getQcToday(paramMap);
				if(null!=tAbbJquancheng){//有待提交和审核不通过的
					Calendar cal = Calendar.getInstance();
					cal.setTime(sdf.parse(tAbbJquancheng.getCreattime()));
					cal.add(Calendar.MINUTE, 30);
					Date d2 = cal.getTime();
					if(tAbbJquancheng!=null){
						resMap.put("canSubmit",1);//可以提交
						resMap.put("jdId",tAbbJquancheng.getId());
						resMap.put("submitTime",sdf.format(d2));
					}else{
						resMap.put("canSubmit",0);//不能提交
					}
				}else{//审核中状态的
					resMap.put("status",2);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else{
			resMap.put("flag",1);//可以领取
		}

		TAbbQuancheng tAbbQuancheng = tAbbQuanchengMapper.selectByPrimaryKey(id);
		TAbbQczifei qczifei = qczifeiMapper.SelectLimitone();
		DecimalFormat df = new DecimalFormat("#.00");
		double money = Double.parseDouble(qczifei.getMoney());
		double pingtaiscore = Double.parseDouble(qczifei.getPingtaiscore());
		double commission = money*((100-pingtaiscore)/100);
		tAbbQuancheng.setCommission(Double.parseDouble(df.format(commission)));
		String username = tAbbQuancheng.getUsername();
		if(null==username||"".equals(username)){
			tAbbQuancheng.setUsername(tAbbQuancheng.getMembername());
			tAbbQuancheng.setUserid(tAbbQuancheng.getMemberid());
		}
		resMap.put("data",tAbbQuancheng);
		resMap.put("success",true);
		resMap.put("msg","获取全城任务详情成功");
		return resMap;
	}


	@Transactional
	public Map<String,Object> releaseQuancheng(TAbbQuancheng tAbbQuancheng){

		Map<String,Object> resMap = new HashMap<>();
		DecimalFormat df = new DecimalFormat("#.00");
		TAbbMember member = memberMapper.selectByPrimaryKey(tAbbQuancheng.getMemberid());

		//查询每条任务的佣金金额
		TAbbQczifei qczifei = qczifeiMapper.SelectLimitone();
		double money = Double.parseDouble(qczifei.getMoney());
		//计算总佣金
		double countBalance = tAbbQuancheng.getNumber()*money;

		if(tAbbQuancheng.getNumber()<20){
			resMap.put("success",false);
			resMap.put("msg","任务条数需大于等于20条");
		}
		tAbbQuancheng.setId(SnowflakeIdWorker.getUUID());
		tAbbQuancheng.setCountblance(Double.valueOf(df.format(countBalance)));
		tAbbQuancheng.setBalance(tAbbQuancheng.getCountblance());
		tAbbQuancheng.setMembername(member.getNicename());
		tAbbQuancheng.setRealnumber(tAbbQuancheng.getNumber());
		tAbbQuancheng.setPaystatus("1");
		tAbbQuancheng.setCreattime(DateUtil.getCurrentDateTimeStr());
		int flag = tAbbQuanchengMapper.insert(tAbbQuancheng);

		TAbbOrder order = new TAbbOrder();
		order.setId(SnowflakeIdWorker.getUUID());
		order.setCreatetime(DateUtil.getCurrentDateTimeStr());
		order.setPaymoney(tAbbQuancheng.getCountblance());
		order.setRenwumoney(tAbbQuancheng.getCountblance());
		order.setObjid(tAbbQuancheng.getId());
		order.setOrderno(OrderNOUtil.getOrderNoByTime());
		order.setType(1);
		order.setUserid(tAbbQuancheng.getMemberid());
		order.setYuliu3(tAbbQuancheng.getTitle());
		order.setYuliu4(member.getNicename());
		order.setPaystatus(0);
		order.setCreatetime(DateUtil.getCurrentDateTimeStr());
		orderMapper.insert(order);

		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","发布成功");
			resMap.put("orderId",order.getId());
		}else{
			resMap.put("success",false);
			resMap.put("msg","发布失败");
		}
		return resMap;
	}


	public List<TAbbQuancheng> getMyQuanchengList(int page,String userid){
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("page",page);
		paramMap.put("userid",userid);
		List<TAbbQuancheng> list = tAbbQuanchengMapper.getMyQuanchengList(paramMap);
		return list;
	}


	public Map<String,Object> getQuanchengPrice(){
		Map<String,Object> resMap = new HashMap<>();
		TAbbQczifei tAbbQczifei = qczifeiMapper.SelectLimitone();
		if(null!=tAbbQczifei){
			resMap.put("quanchengPrice",tAbbQczifei.getMoney());
		}else{
			resMap.put("quanchengPrice",0);
		}
		resMap.put("success",true);
		resMap.put("msg","获取全城任务价格成功");
		return resMap;
	}
}
